<!-- src/layout/index.vue  直接覆盖 -->
<template>
  <div class="layout" :class="{ collapsed }">
    <!-- 侧边栏 -->
    <aside class="sidebar">
      <div class="brand">Houtai</div>
      <nav class="menu">
        <div v-for="m in menus" :key="m.path" class="menu-group">
          <RouterLink
              :to="m.path"
              class="menu-item"
              :class="{ active: route.path === m.path }"
              @click="toggleGroup(m)"
          >
            <span class="label">{{ m.label }}</span>
          </RouterLink>
          <div v-if="m.children?.length" class="submenu" v-show="isExpanded(m)">
            <RouterLink
                v-for="c in m.children"
                :key="c.path"
                :to="c.path"
                class="menu-item sub"
                :class="{ active: route.path === c.path }"
            >
              <span class="label">{{ c.label }}</span>
            </RouterLink>
          </div>
        </div>
      </nav>
      <button class="collapse-btn" @click="collapsed = !collapsed">
        {{ collapsed ? '▶' : '◀' }}
      </button>
    </aside>

    <!-- 主体 -->
    <div class="main">
      <!-- 顶栏（若依三段式） -->
      <header class="topbar">
        <!-- 左：折叠 + 面包屑 -->
        <div class="left">
          <el-icon class="trigger" @click="collapsed = !collapsed">
            <Expand v-if="collapsed" />
            <Fold v-else />
          </el-icon>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item
                v-for="b in breadcrumbs"
                :key="b.path"
                :to="b.path"
            >
              {{ b.title }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <!-- 中：标签页 -->
        <div class="center">
          <div
              v-for="tag in tags"
              :key="tag.path"
              class="tab"
              :class="{ active: route.path === tag.path }"
              @click="router.push(tag.path)"
          >
            {{ tag.title }}
            <el-icon class="close" @click.stop="closeTag(tag)">
              <CloseBold />
            </el-icon>
          </div>
        </div>

        <!-- 右：工具区 -->
        <div class="right">
          <el-tooltip content="搜索" placement="bottom">
            <el-icon class="action"><Search /></el-icon>
          </el-tooltip>
          <el-tooltip content="全屏" placement="bottom">
            <el-icon class="action" @click="toggleFull"><FullScreen /></el-icon>
          </el-tooltip>
          <el-avatar size="small" class="avatar">A</el-avatar>
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="user-name">管理员</span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </header>

      <!-- 内容区 -->
      <section class="content">
        <PageTransition
            name="slide-left"
            :duration="420"
            easing="cubic-bezier(0.22,1,0.36,1)"
            :distance="28"
            mode="out-in"
        >
          <RouterView :key="route.fullPath" />
        </PageTransition>
      </section>

      <!-- 页脚 -->
      <footer class="footer">© 2025 Houtai Admin</footer>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import PageTransition from '../components/PageTransition.vue'
import { Expand, Fold, CloseBold, Search, FullScreen, SwitchButton } from '@element-plus/icons-vue'

/* ---------- 菜单 ---------- */
const collapsed = ref(false)
const route = useRoute()
const router = useRouter()

// 菜单配置（建议后期抽离成 config/menus.ts）
const menus = [
  {
    path: '/dashboard',
    label: '数据统计',
    children: [
      { path: '/dashboard/overview', label: '概览' },
      { path: '/dashboard/charts', label: '图表' }
    ]
  },
  {
    path: '/systemManagement', label: '系统管理',
    children: [
      {path: '/systemManagement/categoryIndex', label: '商品类别管理'},
      {path: '/systemManagement/delayProduct', label: '临期商品管理'},
      {path: '/systemManagement/unitIndex', label: '计量单位管理'},
      {path: '/systemManagement/Product', label: '商品资料管理'},
      {path: '/systemManagement/supplier', label: '供应商资料管理'},
      {path: '/systemManagement/customer', label: '客户资料管理'},
      {path: '/systemManagement/warehouse', label: '仓库管理'},
      {path: '/systemManagement/department', label: '部门管理'}
    ]
  },
  {
    path: '/employee', label: '员工管理',
    children: [
      {path: '/employee/list', label: '员工列表'},

    ]
  },
  {
    path: '/users', label: '用户管理',
    children: [
      { path: '/users/list', label: '用户列表' },
      { path: '/users/roles', label: '角色管理' }
    ]
  },
  {
    path: '/order', label: '订单管理',
    children: [
      { path: '/order/list', label: '订单列表' },
      { path: '/order/return', label: '订单退货' },
    ]
  },
  {
    path: '/procurePlan',
    label: '仓储管理',
    children: [
      { path: '/procurePlan', label: '采购计划' },
      { path: '/orderPlan', label: '采购订单' },
      { path: '/rawMaterial', label: '原材料入库' },
      { path: '/materialRequisition', label: '原材领料' },
      { path: '/productInput', label: '成品入库' },
      { path: '/productShipment', label: '商品发货' }
    ]
  },
  {
    path: '/payMoney',
    label: '财务管理',
    children: [
      { path: '/payMoney', label: '财务付款' }
    ]
  }
]

const groupState = ref<Record<string, boolean>>({})
const isExpanded = (m: any) => {
  const v = groupState.value[m.path]
  return typeof v === 'boolean' ? v : route.path.startsWith(m.path + '/')
}
function toggleGroup(m: any) {
  groupState.value[m.path] = !isExpanded(m)
}

// 标签页逻辑
const tags = ref<{ path: string; title: string }[]>([{ path: '/dashboard', title: '首页' }])
watch(
    () => route.path,
    (path) => {
      const title = (route.meta?.title as string) || '未知'
      if (!tags.value.find(t => t.path === path)) {
        tags.value.push({ path, title })
      }
    },
    { immediate: true }
)
function closeTag(tag: { path: string }) {
  const idx = tags.value.findIndex(t => t.path === tag.path)
  if (idx === -1) return
  tags.value.splice(idx, 1)
  if (tag.path === route.path && tags.value.length > 0) {
    router.push(tags.value[idx - 1]?.path || tags.value[0].path)
  }
}

// 面包屑
const breadcrumbs = computed(() => {
  const matched = route.matched.filter(r => r.meta?.title)
  return matched.map(r => ({
    path: r.path,
    title: r.meta?.title as string
  }))
})

// 全屏
function toggleFull() {
  if (!document.fullscreenElement) document.documentElement.requestFullscreen()
  else document.exitFullscreen()
}

// 用户操作
function handleCommand(cmd: string) {
  if (cmd === 'logout') {
    // 1. 清除sessionStorage中的token（关键）
    sessionStorage.removeItem('token');
    // 2. （可选）清除其他登录相关信息，如记住密码标识
    sessionStorage.removeItem('remember')
    router.push('/login')
  }
}
</script>


<style scoped>
/* ===== 全局变量 - 清新绿生鲜风格 ===== */
:root {
  /* 主色：鲜活青绿色 */
  --primary: #00C88C;
  --primary-hover: #00B27A;
  --primary-light: #E6FFF6;

  /* 背景色 */
  --bg-light: #F8FFF9;        /* 整体背景：极浅绿白 */
  --surface: #FFFFFF;         /* 卡片/面板背景 */
  --sidebar-bg: #FFFFFF;      /* 侧边栏背景 */

  /* 文字颜色 */
  --text-dark: #1F2937;       /* 主文本（深灰）*/
  --text-light: #4B5563;      /* 次要文本 */
  --text-muted: #6B7280;      /* 更淡的文字 */

  /* 边框与分割线 */
  --border-color: #D1E7DD;    /* 浅绿边框 */
  --line: rgba(0, 200, 140, 0.1);

  /* 阴影 */
  --shadow: 0 4px 12px rgba(0, 200, 140, 0.08);
  --shadow-sm: 0 1px 4px rgba(0, 200, 140, 0.06);
}

/* ===== 整体框架 ===== */
.layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
  background: var(--bg-light);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  color: var(--text-dark);
  transition: all 0.3s ease;
}
.layout.collapsed {
  grid-template-columns: 72px 1fr;
}

/* ===== 侧边栏优化：毛玻璃 + 清新绿 ===== */
.sidebar {
  background: var(--sidebar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: 100;
}

.brand {
  font-size: 20px;
  font-weight: 700;
  padding: 20px 16px;
  color: var(--primary);
  text-align: center;
  letter-spacing: 0.8px;
  background: transparent;
}

/* 菜单区域 */
.menu {
  flex: 1;
  padding: 12px 8px;
  overflow-y: auto;
}
.menu-group {
  margin-bottom: 4px;
}

/* 一级菜单项 */
.menu-item {
  display: flex;
  align-items: center;
  padding: 10px 14px;
  border-radius: 10px;
  color: var(--text-light);
  font-size: 14px;
  transition: all 0.2s ease;
  gap: 10px;
}
.menu-item:hover {
  background: rgba(0, 200, 140, 0.1);
  color: var(--primary);
}
.menu-item.active {
  background: var(--primary);
  color: white;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0, 200, 140, 0.2);
}

/* 二级菜单样式 */
.submenu {
  padding: 6px 0 6px 10px;
  border-left: 2px solid var(--primary);
}
.menu-item.sub {
  padding: 8px 12px;
  font-size: 13px;
  color: var(--text-light);
}
.menu-item.sub:hover {
  color: var(--primary);
  background: transparent;
}
.menu-item.sub.active {
  color: var(--primary);
  background: transparent;
  font-weight: 600;
}

/* 折叠按钮 */
.collapse-btn {
  margin: 12px 8px 16px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border-color);
  //background: var(--surface);
  color: var(--text-light);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s;
}
.collapse-btn:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* ===== 主体区域 ===== */
.main {
  display: grid;
  grid-template-rows: 56px 1fr 48px;
  min-height: 100vh;
  overflow: hidden;
}

/* ===== 顶栏（Topbar） - 毛玻璃半透明效果 ===== */
.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border-color);
  box-shadow: 0 1px 8px rgba(0, 0, 0, 0.03);
  z-index: 99;
}

.left {
  display: flex;
  align-items: center;
  gap: 12px;
}
.trigger {
  font-size: 18px;
  cursor: pointer;
  color: var(--text-light);
}
.trigger:hover {
  color: var(--primary);
}

/* 面包屑 */
.el-breadcrumb {
  font-size: 14px;
}
.el-breadcrumb__inner {
  color: var(--text-light) !important;
}
.el-breadcrumb__inner:hover {
  color: var(--primary) !important;
}

/* 标签页 Tabs */
.center {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
  max-width: 50%;
}
.center::-webkit-scrollbar {
  display: none;
}
.tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
  //background: var(--surface);
  color: var(--text-light);
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}
.tab:hover {
  background: #ffffff;
  border-color: var(--border-color);
}
.tab.active {
  background: var(--primary);
  color: white;
  font-weight: 500;
}
.close {
  font-size: 12px;
  opacity: 0.6;
}
.close:hover {
  opacity: 1;
  color: #ff6b6b;
}

/* 右侧功能区 */
.right {
  display: flex;
  align-items: center;
  gap: 12px;
}
.action {
  font-size: 18px;
  color: var(--text-light);
  cursor: pointer;
  transition: color 0.2s;
}
.action:hover {
  color: var(--primary);
}
.avatar {
  width: 32px;
  height: 32px;
  background: var(--primary);
  color: white;
  font-weight: bold;
  box-shadow: 0 2px 6px rgba(0, 200, 140, 0.3);
}
.user-name {
  font-size: 14px;
  color: var(--text-dark);
  font-weight: 500;
}

/* ===== 内容区 ===== */
.content {
  padding: 20px;
  background: var(--bg-light);
  overflow-y: auto;
}

/* 页脚 */
.footer {
  display: grid;
  place-items: center;
  font-size: 12px;
  color: var(--text-light);
  border-top: 1px solid var(--border-color);
  //background: var(--surface);
}

/* ===== 响应式优化 ===== */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: 72px 1fr;
  }
  .submenu {
    display: none;
  }
  .center {
    max-width: 40%;
  }
}

.layout.collapsed .submenu {
  display: none;
}
.layout.collapsed .brand {
  text-align: center;
}
</style>